package no.ntnu.fp.watch.gui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;

import javax.swing.AbstractListModel;

import no.ntnu.fp.model.Database;
import no.ntnu.fp.model.Well;

public class EventLogListModel extends AbstractListModel implements PropertyChangeListener{
	
	ArrayList<Event> events;
	Database database;
	private PropertyChangeSupport propChangeSupp;
	
	
	public EventLogListModel(ArrayList<Event> eventlist, Database db){
		this.events = eventlist;
		this.database = db;
		this.propChangeSupp = new PropertyChangeSupport(this);
	}
	
	@Override
	public Object getElementAt(int arg0) {
		return events.get(arg0);
	}

	@Override
	public int getSize() {
		if(events != null){
			return events.size();
		}
		return 0;
		
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		Well w = (Well)evt.getSource();
		
		
		if(evt.getPropertyName() == Well.STATUS_PROPERTY){
			//TODO: Fiks at eventloggen oppdateres med nye events i realtime
			//events = database.getEvents(w.getId());
			
			if (events != null && events.size() > 0) {
				//ArrayList<Event> e = database.getLatestEvents(events.get(events.size()-1).getEventid(), w.getId());
				
//				for (int i = 0; i < e.size(); i++) {
//					//events.add(e.get(i));
//					System.out.println("Event: " +e.get(i).getEventid());
//				}
			}
			else {
				//events = database.getEvents(w.getId());
			}
			
			
			
			//propChangeSupp.firePropertyChange(evt);

			
		}
		
	}

}
